﻿using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace 中文简体转繁体_繁体转简体_
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        #region 主体方法

        [DllImport("kernel32.dll", EntryPoint = "LCMapStringA")]
        public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest);

        // 繁体转简体
        const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
        // 简体转繁体
        const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;

        /// <summary>
        /// 简体转繁体
        /// </summary>
        /// <param name="source">字符串</param>
        /// <param name="type">类型(LCMAP_SIMPLIFIED_CHINESE | LCMAP_TRADITIONAL_CHINESE)</param>
        /// <returns></returns>
        public static string ConvertToTraditional(string source, int type)
        {
            byte[] srcByte2 = Encoding.Default.GetBytes(source);
            byte[] desByte2 = new byte[srcByte2.Length];
            LCMapString(2052, type, srcByte2, -1, desByte2, srcByte2.Length);
            string des2 = Encoding.Default.GetString(desByte2);
            return des2;
        }

        #endregion

        public MainWindow()
        {
            InitializeComponent();
        }

        private void CN_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.TW.Text = ConvertToTraditional(this.CN.Text, LCMAP_TRADITIONAL_CHINESE);
        }
    }
}
